<template>
  <div class="pay-container">
    <!-- 生成微信支付二维码 -->
    <div class="erwei">
      <img :src="qrUrl" alt="" />
    </div>
    <svg class="icon" aria-hidden="true">
      <use xlink:href="#icon-wechat_pay"></use>
    </svg>
    <span>使用微信进行支付</span>
    <el-descriptions title="订单详情">
      <el-descriptions-item label="订单号">
        {{ order_detail.order_id }}
      </el-descriptions-item>
      <el-descriptions-item label="创建时间">
        {{ order_detail.create_time }}
      </el-descriptions-item>
      <el-descriptions-item label="产品名称">
        {{ order_detail.orderDetail[0].productName }}
      </el-descriptions-item>
      <el-descriptions-item label="商品描述">
        {{ order_detail.orderDetail[0].description }}
      </el-descriptions-item>
      <el-descriptions-item label="购买数量">
        {{ order_detail.orderDetail[0].quantity }}
      </el-descriptions-item>
      <el-descriptions-item label="购买用户">
        {{ order_detail.user_nickname }}
      </el-descriptions-item>
      <el-descriptions-item label="手机号">
        {{ order_detail.user_phone ? order_detail.user_phone : "没有手机号码" }}
      </el-descriptions-item>
      <el-descriptions-item label="地址">
        {{ order_detail.address }}
      </el-descriptions-item>
      <el-descriptions-item label="总价格">
        {{ order_detail.total_fee }}
      </el-descriptions-item>
      <el-descriptions-item label="计时器">
        ---{{ queryInterval }}----
      </el-descriptions-item>
    </el-descriptions>
  </div>
</template>

<script>
import * as payApi from "@/api/pay";
import qr from "qrcode";
import { mapState } from "vuex";

export default {
  data() {
    return {
      order_detail: {},
      qrUrl: "",
      order_id: "",
    };
  },
  computed: {
    ...mapState(["userInfo"]),
  },
  methods: {
    queryOrderStatus() {
      this.queryInterval = setInterval(async () => {
        let res = await payApi.queryOrderStatus(this.order_id);
        console.log(res);
        if (res.data.state) {
          this.$router.push({ name: "paySuccess" });
        }
      }, 1000);
    },
  },
  async created() {
    this.order_id = this.$route.query.order_id;
    if (!this.order_id) return this.$router.push("/vipmanager/charge");

    let res = await Promise.all([
      payApi.getAllOrder(this.order_id),
      payApi.startPay(this.order_id),
    ]);

    this.queryOrderStatus();
    console.log(res);
    this.order_detail = res[0].data.data;
    let payUrl = res[1].data.code_url;
    if (!res[1].data.state) return this.$router.push("/vipmanager/charge");
    qr.toDataURL(payUrl, (err, qrImgUrl) => {
      if (err) return this.$messageBox.error(err);

      this.qrUrl = qrImgUrl;
    });
  },
  beforeDestroy() {
    clearInterval(this.queryInterval);
  },
};
</script>
